﻿namespace Redmist.UsageMonitor.WebIO
{
    using System;
    using System.IO;
    using System.Net;        
    
    public static class JsonRetriever
    {        
        public static UsageRetrievalResult RetrieveUsage(string serviceNumber, string password, string url)
        {
            if (serviceNumber == null)
            {
                throw new ArgumentNullException("serviceNumber");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            UsageRetrievalResult usageRetrievalResult = new UsageRetrievalResult();            

            try
            {
                var fullUrl = url + "?phoneNumber=" + serviceNumber + "&password=" + password;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl);
                request.Timeout = 60000;
                request.Method = "GET";
                request.AllowAutoRedirect = false;
                var response = request.GetResponse();
                string textResponse;
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        textResponse = streamReader.ReadToEnd();
                    }
                }
                var aanetUsageModel = JsonUtility.Deserialise<UsageData.DataUsageModel>(textResponse);
                if (aanetUsageModel.IsAuthenticationFailure)
                {
                    usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.AuthenticationError;
                }
                else if (aanetUsageModel.IsUsageInformationAvailable)
                {
                    usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.OK;
                    usageRetrievalResult.MonthlyUsageResult = UsageData.MonthlyUsage.ConvertAanetUsageModelToMonthlyUsage(aanetUsageModel);
                }
                else
                {
                    usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ConnectionError;
                }
            }
            catch (WebException webException)
            {
                System.Diagnostics.Trace.WriteLine("webException.message: " + webException.Message);
                switch (webException.Status)
                {
                    case WebExceptionStatus.Timeout:
                        usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.Timeout;
                        break;
                    default:
                        usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ConnectionError;
                        break;
                }
            }
            catch (System.Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Exception: " + e.Message);
                usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ProtocolError;
            }

            return usageRetrievalResult; 
        }
        
    }
}
